import request from '@/config/axios'
import { MicroServicePrefix } from '@/api/base/model'

export interface RoleVO {
  name: string
  intro: string
}

export interface RoleAuthorizeVO {
  roleId: number
  resourceId: string
}

// 查询角色列表
export const getRolePage = async (data: QueryCriteria) => {
  return await request.post({ url: `${MicroServicePrefix.FACILITIES}/role/query`, data })
}

// 查询角色（精简)列表
export const getSimpleRoleList = async (): Promise<RoleVO[]> => {
  return await request.get({ url: '/system/role/simple-list' })
}

// 查询角色详情
export const getRole = async (id: number) => {
  return await request.get({ url: `${MicroServicePrefix.FACILITIES}/role/${id}` })
}

// 新增角色
export const createRole = async (data: RoleVO) => {
  return await request.post({ url: `${MicroServicePrefix.FACILITIES}/role`, data })
}

// 修改角色
export const updateRole = async (id: number, data: RoleVO) => {
  return await request.put({ url: `${MicroServicePrefix.FACILITIES}/role/${id}`, data })
}

// 删除角色
export const deleteRole = async (id: number) => {
  return await request.delete({ url: `${MicroServicePrefix.FACILITIES}/role/${id}` })
}

// 获取已授权权限代码
export const getAssignedResourceCodes = async (id: number): Promise<string[]> => {
  return await request.get({ url: `${MicroServicePrefix.FACILITIES}/role/${id}/authorized` })
}

// 添加授权
export const addAuthorized = async (data: RoleAuthorizeVO) => {
  return await request.put({ url: `${MicroServicePrefix.FACILITIES}/role/authorize`, data })
}

// 移除授权
export const removeAuthorized = async (data: RoleAuthorizeVO) => {
  return await request.delete({ url: `${MicroServicePrefix.FACILITIES}/role/authorize`, data })
}
